<?php

namespace App\Http\Controllers\api;


use App\Http\Controllers\Controller;
use App\Models\Renting;
use App\Service\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class UserSignIn extends Controller
{
    /**
     * @return array
     * User: 冯保杰
     * Date: 2022/11/14
     * Time: 8.37
     * 获取用户签到列表接口
     */
    public function getUserSignList()
    {
        try {
            $uid = \request()->get('uid', 1);
            //获取当前时间
            $now = date('Ym');
            //拼接bitmap的key
            $key = $uid . ':' . $now;
            $arr = [];
            for ($i = 1; $i <= date('d'); $i++) {
                $arr[$i] = Redis::getbit($key, $i);
            }
            return CommonService::success(200, '获取用户月签到信息', $arr);
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }

    /**
     * @return array
     * User: 冯保杰
     * Date: 2022/11/11
     * Time: 14：37
     * 用户签到接口
     */
    public function signIn()
    {
        //解码token获取用户id
        $uid = \request()->get('uid', 1);
        try {
            //获取当前时间
            $now = date('Ym');
            $day = date('d');
            //拼接bitmap的key
            $key = $uid . ':' . $now;
            //查看今日是否签到过

            $date = Redis::getbit($key, $day);
            if ($date) return CommonService::fail(9999, '已经签过了');
            //获取昨天时间
            $lastDay = date("d", strtotime("-1 day"));
            //查看昨天是否签到
            $lastDate = Redis::getbit($key, $lastDay);
            $user = Renting::where('id', $uid)->first()->toArray();//获取这个人的信息
            if ($lastDate) {
                //积分叠加   bitmap存已签到
                $integral = $user['integral'] + 10 + ($user['con_sign_in_day'] * 15);//config表中获取每日增长
                $user['integral'] = $integral;//增加积分
                $user['con_sign_in_day'] = $user['con_sign_in_day'] + 1;//连续签到次数+1
                Renting::update($user);
                Redis::setbit($key, $day, 1);
                return CommonService::success(200, '连续签到');
            }
            //昨天未签到(断签)
            $user['con_sign_in_day'] = 1;//连续签到次数重置为1
            $user['integral'] = $user['integral'] + 10;//第一天+10
            Renting::where('id', $uid)->update($user);
            Redis::setbit($key, $day, 1);
            return CommonService::success(200, '已断签，签到成功');
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }

    /**
     * @return array
     * User: 冯保杰
     * Date: 2022/11/11
     * Time: 15:16
     * 用户补签接口
     */
    public function mendSignIn()
    {
        $uid = \request()->get('uid', '1');
        $signInDay = \request()->get('mendSignInDay');//需要补签的天数
        if ($signInDay > date('d')) return CommonService::fail(9999, '无法签到');
        try {
            //获取当前时间
            $now = date('Ym');
            $day = date('d');
            //拼接bitmap的key
            $key = $uid . ':' . $now;
            if (Redis::getbit($key, $signInDay) == 1) return CommonService::fail(9999, "'第' . $signInDay . '天已签到,无需补签'");
            Redis::setbit($key, $signInDay, 1);//补签成功
            $count = 0;//计数
            //计算补签当天 --- 今日的连续签到次数
            for ($i = date('d'); $i <= $day; $i++) {
                if (Redis::getbit($key, $i) == 0) break;
                $count = $count + 1;
            }
            //计算补签当天 --- 总共签到次数
            for ($i = date('d') - 1; $i >= 1; $i--) {
                if (Redis::getbit($key, $i) == 0) break;
                $count = $count + 1;
            }
            $userDb = Renting::where('id', $uid)->first()->toArray();//   获取用户信息，并修改用户连续签到天数
            $userDb['con_sign_in_day'] = $count;
            Renting::where('id', $uid)->update($userDb);
            return CommonService::success(200, "恭喜您补签成功，目前连续签到天数.$count");
        } catch (\Exception $e) {
            return CommonService::fail(9999, $e->getMessage());
        }
    }

}

